import request from './request.js';
import { baseUrl, platformToken, platform as pl } from '../config.js';
import association from '../store';

export default {
  // 商品列表
  goodsList: (data) =>
    request({
      url: 'api/goods/goodsList',
      method: 'GET',
      data,
    }),

  // 团购列表
  groupList: (data) =>
    request({
      url: 'api/goods/groupList',
      method: 'GET',
      data,
    }),

  // 门店列表
  storeList: (data) =>
    request({
      url: 'api/goods/storeList',
      method: 'GET',
      data,
    }),

  // 评论新街口 /api/find/commentList
  findCommentList: (data) =>
    request({
      url: '/api/find/commentList',
      method: 'GET',
      data,
    }),
  // 删除
  removeContent: (data) =>
    request({
      url: 'api/find/delFind',
      method: 'GET',
      data,
    }),
  // 置顶
  stickyApi: (data) =>
    request({
      url: 'api/my_find/pinned',
      method: 'POST',
      data,
      custom: {
        auth: true,
      },
    }),
  // 获取评论条数
  commonTalApi: (data) =>
    request({
      url: 'api/find/getCommentNum',
      method: 'GET',
      data,
    }),
  // 分享
  send_share: (data) =>
    request({
      url: 'api/share/send',
      method: 'POST',
      data,
      custom: {
        auth: true,
      },
    }),
  // 分享列表
  shareList: (data) =>
    request({
      url: '/api/share/share',
      method: 'GET',
      data,
    }),

  // 登录接口
  loginApi: () =>
    request({
      url: 'api/user/thirdLogin',
      method: 'GET',
      header: {
        Authorization: uni.getStorageSync(platformToken),
      },
      custom: {
        auth: true,
      },
    }),
  // 登录方法
  login: async () => await association().login(),
  // 首页小程序列表
  index: (platform = pl, data) =>
    request({
      url: 'api/index/index',
      method: 'GET',
      data,
      platform,
    }),
  // 获取二级分类
  getCategory: (data) =>
    request({
      url: 'api/index/getCategory',
      method: 'GET',
      data,
    }),
  //获取话题列表
  gettalkList: (data) =>
    request({
      url: 'api/topic/list',
      method: 'GET',
      data,
    }),
  // 商学院列表 api/find_new/listNew
  getshopList: (data) =>
    request({
      url: 'api/topic/list',
      method: 'GET',
      data,
    }),
  // 话题详情
  topicDetails: (data) =>
    request({
      url: 'api/topic/details',
      method: 'GET',
      data,
    }),
  // 话题收藏和取消收藏
  topicCollection: (data) =>
    request({
      url: 'api/topic/collection',
      method: 'POST',
      data,
      custom: {
        auth: true,
      },
    }),
  // 话题转发记录
  topicForward: (data) =>
    request({
      url: 'api/topic/forward',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 话题评论
  topicComment: (data) =>
    request({
      url: 'api/topic/comment',
      method: 'POST',
      data,
      custom: {
        auth: true,
      },
    }),
  // 话题删除我的评论
  topicDelComment: (data) =>
    request({
      url: 'api/topic/delComment',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 话题点赞&&取消
  topicLike: (data) =>
    request({
      url: 'api/topic/like',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 发现详情
  getFindDetails: (data) =>
    request({
      url: 'api/find_new/details',
      method: 'GET',
      data,
    }),
  // 发现列表
  getfindList: (data) =>
    request({
      url: 'api/find_new/listNew',
      method: 'GET',
      data,
    }),

  getfindUserList: (data) =>
    request({
      url: 'api/my_find/list',
      method: 'GET',
      data,
    }),
  // 话题点赞&&取消
  findLike: (data) =>
    request({
      url: 'api/find/like',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 广场列表
  getsquareList: (data) =>
    request({
      url: 'api/find_new/listNew',
      method: 'GET',
      header: {
        platform: '',
      },
      data: {
        type: 'all',
        ...data,
      },
    }),
  //发现评论
  findComment: (data) =>
    request({
      url: 'api/find/comment',
      method: 'POST',
      data,
      custom: {
        auth: true,
      },
    }),
  // 发现转发记录
  findForward: (data) =>
    request({
      url: 'api/find/forward',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 发现删除我的评论
  findDelComment: (data) =>
    request({
      url: 'api/find/delComment',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 发现评论点赞
  findCommentLike: (data) =>
    request({
      url: 'api/find/commentLike',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 发现文章收藏&&取消
  findCollection: (data) =>
    request({
      url: 'api/find/collection',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 查询平台对应下的商品、团购、门店列表
  getPromotionInfo: (data) =>
    request({
      url: 'api/find/getPromotionInfo',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 发布(续写)日记 、 等发现文章
  addFind: (data) =>
    request({
      url: 'api/find_new/addFind',
      method: 'POST',
      data,
      custom: {
        auth: true,
      },
    }),
  // 删除发现文章
  delFind: (data) =>
    request({
      url: 'api/find/delFind',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  // 我的发现文章列表
  myFindList: (data) =>
    request({
      url: 'api/find/myFindList',
      method: 'GET',
      data,
      custom: {
        auth: true,
      },
    }),
  //上传
  upload: (file) => {
    return new Promise((resolve, reject) => {
      if (!uni.getStorageSync('associationToken')) {
        association()
          .login()
          .then(() => {
            uni.uploadFile({
              url: baseUrl + 'api/common/upload',
              filePath: file,
              name: 'file',
              header: {
                Accept: 'text/json',
                token: uni.getStorageSync('associationToken'),
              },
              success: (uploadFileRes) => {
                let result = JSON.parse(uploadFileRes.data);
                if (result.code == 1) {
                  resolve(result.data);
                } else {
                  uni.showToast({
                    icon: 'none',
                    title: result.msg,
                  });
                  resolve(false);
                }
              },
              fail: (error) => {
                console.log('上传失败：', error);
                return resolve(false);
              },
              complete: () => {
                // uni.hideLoading();
              },
            });
          });
      } else {
        uni.uploadFile({
          url: baseUrl + 'api/common/upload',
          filePath: file,
          name: 'file',
          header: {
            Accept: 'text/json',
            token: uni.getStorageSync('associationToken'),
          },
          success: (uploadFileRes) => {
            let result = JSON.parse(uploadFileRes.data);
            if (result.code == 1) {
              resolve(result.data);
            } else {
              uni.showToast({
                icon: 'none',
                title: result.msg,
              });
              resolve(false);
            }
          },
          fail: (error) => {
            console.log('上传失败：', error);
            return resolve(false);
          },
          complete: () => {
            // uni.hideLoading();
          },
        });
      }
    });
  },
  info: {
    getInfo: (data) =>
      request({
        url: 'api/index/getInfo',
        method: 'GET',
        data,
        custom: {
          showError: false,
          auth: true,
        },
      }),
    collection: (data) =>
      request({
        url: 'api/find/collection',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    operate: (data) =>
      request({
        url: 'api/care/operate',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    careList: (data) =>
      request({
        url: 'api/care/careList',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    fansList: (data) =>
      request({
        url: 'api/care/fansList',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    detail: (data) =>
      request({
        url: 'api/find/detail',
        method: 'POST',
        data,
        custom: {
          auth: true,
        },
      }),
    collectionList: (data) =>
      request({
        url: 'api/find/collectionList',
        method: 'POST',
        data,
        custom: {
          auth: true,
        },
      }),
    // 关注列表
    getFollowList: (data) =>
      request({
        url: 'api/my_find/follow',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    // 粉丝列表
    getFansList: (data) =>
      request({
        url: 'api/my_find/fans',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    // 主页信息
    getUserInfo: (data) =>
      request({
        url: 'api/my_find/index',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
  },
  // 新增接口
  publish: {
    // 获取主题
    subject: (data) =>
      request({
        url: 'api/tag/list',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    friend: (data) =>
      request({
        url: 'api/tag/getFriendsList',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
  },
  // 新增 个人中心
  user: {
    details: (data) =>
      request({
        url: 'api/my_find/index',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    follow: (data) =>
      request({
        url: 'api/my_find/care',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    list: (data) =>
      request({
        url: 'api/my_find/list',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    info: (data) =>
      request({
        url: 'api/my_find/profile',
        method: 'GET',
        data,
        custom: {
          auth: true,
        },
      }),
    edit_info: (data) =>
      request({
        url: 'api/user/profile',
        method: 'POST',
        data,
        custom: {
          auth: true,
        },
      }),
    categoryList: () =>
      request({
        url: 'api/my_find/category',
        method: 'GET',
        custom: {
          auth: true,
        },
      }),
  },
};
